package blog

import (
	"Blog/database"
	"Blog/model"
	"fmt"
	"github.com/kataras/iris/v12"
)

/**
文章表结构体
*/
type article struct {
	Id, Title, Desc, Img, CreateTime, MenuId, Hits string
}

func Index(ctx iris.Context) {
	ctx.ViewData("Config", model.Configs)
	ctx.ViewData("PrevPage", "1")
	ctx.ViewData("NextPage", "2")
	menuid := "0"
	line := article{}
	var articles []article
	rows, err := database.Db.Query("SELECT `id`,`menu_id`,`title`,`desc`,`img`,`create_time`,`hits` FROM amibk_article ORDER BY create_time DESC LIMIT 0,6")
	articleLen := 0
	for rows.Next() {
		articleLen++
		err := rows.Scan(&line.Id, &line.MenuId, &line.Title, &line.Desc, &line.Img, &line.CreateTime, &line.Hits)
		if err != nil {
			return
		}
		articles = append(articles, line)
	}
	ctx.ViewData("articles", articles)
	ctx.ViewData("articleLen", articleLen)
	ctx.ViewData("menu", model.Menus)
	ctx.ViewData("MenuId", menuid)
	err = ctx.View("index.html")
	if err != nil {
		fmt.Println(err)
		return
	}
}
